package 网络编程.com.chen.api;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName Api_.java
 * @Description TODO
 * @createTime 2023年05月01日 10:46:00
 */
public class Api {
    public static void main(String[] args) {
        //获取本机的InetAddress对象
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);//DESKTOP-466O49R/192.168.1.101

            //2.根据指定主机名 获取 InetAddress对象
            InetAddress host1 = InetAddress.getByName("DESKTOP-466O49R");
            System.out.println("host1"+host1);

            //3.根据域名返回 InetAddress对象，比如获取www.baidu.com对应的InetAddress对象
            InetAddress host2 = InetAddress.getByName("www.baidu.com");
            System.out.println("host2 "+host2);

            //4.通过 InetAddress对象，获取对应的地址
            String hostAddress = host2.getHostAddress();
            System.out.println("host2 对应的ip = "+hostAddress);

            //5.通过InetAddress 对象，获取对应的主机名/或者的域名
            String hostName = host2.getHostName();
            System.out.println("host2对应的主机名/域名= " + hostName);


        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
